// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// 引入vuex
import store from './store/vuex'
import Api  from '@/api/Api'

// 引入统一样式
import './assets/styles/reset.css'
import './assets/styles/app.scss'

// 引入富文本
import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import  '@/assets/iconfont/iconfont.css'

Vue.use(VueQuillEditor)

Vue.config.productionTip = false
Vue.use(ElementUI, { size: 'small' })


// 路由拦截
router.beforeEach((to, from, next) => {
  if (to.path == "/") { 
    next();
  }
  else { 
    //验证token是否存在
    if (!store.state.token) {  
      ElementUI.MessageBox.alert('登录信息已过期', '提示', {
        confirmButtonText: '确定',
        callback: action => {
          next({
            path: '/',   //跳去登录
            // query: {redirect: '/'}   //携带参数
          })
        }
      });

    }
    else {
      let user = store.state.user
      if(user.userId == undefined || user ==null) {
        //重新获取当前用户信息
        Api.detail().then( res => {
          if(res.data.code == 0){
              //设置store token值
              store.commit("setUser",res.data.data)
              next();
          }else{
            //不合法
            ElementUI.MessageBox.alert(res.data.msg, '提示', {
              confirmButtonText: '确定',
              callback: action => {
                next({path: '/'})
              }
            });
          }
        })
      }else{
        next()
      }

    }
  }
})

// 
router.afterEach((to, from) => {})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
